﻿
using CoreWeb.Authentication;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Text;

namespace CoreWeb.Authentication
{
    class AuthFilterAttribute : ActionFilterAttribute
    {

        private LoginUserManager loginUserManager;
        public AuthFilterAttribute(LoginUserManager loginUserManager)
        {
            this.loginUserManager = loginUserManager;
        }

        /// <summary>
        /// 是否可匿名访问
        /// </summary>
        public bool Anonymous { get; set; }

        public override void OnActionExecuting(ActionExecutingContext context)
        {
            if (Anonymous) return;
            var user = loginUserManager.GetUser(LoginUserManager.DefaultTokenKey);
            if (user == null)
            {
                var ro = new LoginUrlResult { Msg = "nologin", LoginUrl = "" };
                context.Result = new JsonResult(ro);
            }
            base.OnActionExecuting(context);


            context.HttpContext.Request.Body.Position = 0;


        }
    }
}
